package com.zzyk.main.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtils {
    private static String regexExt = "\\.(.*)$";
    private static Pattern p = Pattern.compile(regexExt);

    public static String getExt(String filename) {
        Matcher m = p.matcher(filename);
        if (m.find()) {
            return m.group(1);
        }
        return null;
    }

    public static String getExt(File file) {
        return getExt(file.getName());
    }

    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public static InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
          e.printStackTrace();
        }
        return null;
    }

}
